home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 151-175 / scopedisk157 / icon2c / icon2c.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  9KB  |  327 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1990 by MXM
  4.  *
  5.  *    Name .....: RemapIcon.c
  6.  *    Created ..: Tuesday 26-Jun-90 14:19
  7.  *    Revision .: 1
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    26-Jun-90       Olsen           Created this file!
  12.  *
  13.  * $Revision Header ********************************************************/
  14.  
  15.     /* Included header files. */
  16.  
  17. #define __NO_PRAGMAS 1
  18.  
  19. #include <workbench/workbench.h>
  20. #include <libraries/dosextens.h>
  21. #include <libraries/arpbase.h>
  22. #include <functions.h>
  23.  
  24.     /* Arp cli variables. */
  25.  
  26. char *CLI_Template    = "From,To,I=Info/S";
  27. char *CLI_Help        = "\nUsage: \33[1mIcon2C\33[0m [Source file name without \".info\"] [Destination file] [Info]\n";
  28.  
  29.     /* Argument vector offsets. */
  30.  
  31. #define ARG_FROM    1
  32. #define ARG_TO        2
  33. #define ARG_INFO    3
  34.  
  35.     /* Icon library base. */
  36.  
  37. struct Library    *IconBase;
  38.  
  39.     /* Image2C(struct Image *IconImage,char *Name,BYTE Number,BPTR ImageFile):
  40.      *
  41.      *    Converts an image into 'C' sourcecode.
  42.      */
  43.  
  44. VOID
  45. Image2C(struct Image *IconImage,char *Name,BYTE Number,BPTR ImageFile)
  46. {
  47.     SHORT     i,j,LastWord;
  48.     USHORT    *ImageData = IconImage -> ImageData;;
  49.  
  50.         /* Remember last word to be written. */
  51.  
  52.     LastWord = ((IconImage -> Width + 15) >> 4) * IconImage -> Height;
  53.  
  54.         /* Write the header. */
  55.  
  56.     FPrintf(ImageFile,"USHORT %s%ldData[%ld] =\n{",Name,Number,LastWord * IconImage -> Depth);
  57.  
  58.         /* Write the bitmap information contained in the
  59.          * single bit planes.
  60.          */
  61.  
  62.     for(j = 0 ; j < IconImage -> Depth ; j++)
  63.     {
  64.         for(i = 0 ; i < LastWord ; i++)
  65.         {
  66.             if(!(i % 8))
  67.                 FPrintf(ImageFile,"\n\t");
  68.  
  69.             FPrintf(ImageFile,"0x%04lx",*ImageData++);
  70.  
  71.             if(i != LastWord - 1)
  72.                 FPrintf(ImageFile,",");
  73.         }
  74.  
  75.         if(j != IconImage -> Depth - 1)
  76.             FPrintf(ImageFile,",\n");
  77.     }
  78.  
  79.         /* Add the image structure itself. */
  80.  
  81.     FPrintf(ImageFile,"\n};\n\n");
  82.  
  83.     FPrintf(ImageFile,"struct Image %s%ldImage =\n{\n",Name,Number);
  84.  
  85.     FPrintf(ImageFile,"\t%ld,%ld,\n",IconImage -> LeftEdge,IconImage -> TopEdge);
  86.     FPrintf(ImageFile,"\t%ld,%ld,%ld,\n",IconImage -> Width,IconImage -> Height,IconImage -> Depth);
  87.     FPrintf(ImageFile,"\t(USHORT *)&%s%ldData[0],\n",Name,Number);
  88.  
  89.     FPrintf(ImageFile,"\t0x%02lx,0x00,\n",(1 << IconImage -> Depth) - 1);
  90.     FPrintf(ImageFile,"\t(struct Image *)NULL\n");
  91.  
  92.     FPrintf(ImageFile,"};\n\n");
  93. }
  94.  
  95.     /* Icon2C(struct DiskObject *Icon,char *Name,BPTR IconFile):
  96.      *
  97.      *    Convert an icon file into 'C' sourcecode, creates
  98.      *    image run dumps and associated icon structures.
  99.      */
  100.  
  101. VOID
  102. Icon2C(struct DiskObject *Icon,char *Name,BPTR IconFile)
  103. {
  104.     STATIC char *WB_Types[7] =
  105.     {
  106.         "WBDISK",
  107.         "WBDRAWER",
  108.         "WBTOOL",
  109.         "WBPROJECT",
  110.         "WBGARBAGE",
  111.         "WBDEVICE",
  112.         "WBKICK"
  113.     };
  114.  
  115.     struct DrawerData     *Drawer = Icon -> do_DrawerData;
  116.     char            **ToolTypes = Icon -> do_ToolTypes;
  117.  
  118.     Puts("\n\n\tCreating icon image...");
  119.  
  120.         /* Create the header. */
  121.  
  122.     FPrintf(IconFile,"#include <workbench/workbench.h>\n\n");
  123.  
  124.         /* Create the first icon image. */
  125.  
  126.     Image2C((struct Image *)Icon -> do_Gadget . GadgetRender,Name,1,IconFile);
  127.  
  128.         /* Create alternate icon image (if present). */
  129.  
  130.     if(Icon -> do_Gadget . Flags & GADGHIMAGE)
  131.     {
  132.         Puts("\tCreating alternate icon image...");
  133.         Image2C((struct Image *)Icon -> do_Gadget . SelectRender,Name,2,IconFile);
  134.     }
  135.  
  136.         /* Create the drawer data if neccessary. */
  137.  
  138.     if(Drawer && (Icon -> do_Type == WBDISK || Icon -> do_Type == WBDRAWER || Icon -> do_Type == WBGARBAGE))
  139.     {
  140.         Puts("\tCreating icon drawer data...");
  141.  
  142.         FPrintf(IconFile,"struct DrawerData %sDrawer =\n{\n",Name);
  143.         FPrintf(IconFile,"\t%ld,%ld,\n",Drawer -> dd_NewWindow . LeftEdge,Drawer -> dd_NewWindow . TopEdge);
  144.         FPrintf(IconFile,"\t%ld,%ld,\n",Drawer -> dd_NewWindow . Width,Drawer -> dd_NewWindow . Height);
  145.         FPrintf(IconFile,"\t%ld,%ld,\n",Drawer -> dd_NewWindow . DetailPen,Drawer -> dd_NewWindow . BlockPen);
  146.         FPrintf(IconFile,"\t0x%08lx,\n",Drawer -> dd_NewWindow . IDCMPFlags);
  147.         FPrintf(IconFile,"\t0x%08lx,\n",Drawer -> dd_NewWindow . Flags);
  148.         FPrintf(IconFile,"\t(struct Gadget *)NULL,\n");
  149.         FPrintf(IconFile,"\t(struct Image *)NULL,\n");
  150.  
  151.         if(Drawer -> dd_NewWindow . Title)
  152.             FPrintf(IconFile,"\t(UBYTE *)\"%s\",\n",Drawer -> dd_NewWindow . Title);
  153.         else
  154.             FPrintf(IconFile,"\t(UBYTE *)NULL,\n");
  155.  
  156.         FPrintf(IconFile,"\t(struct Screen *)NULL,\n");
  157.         FPrintf(IconFile,"\t(struct BitMap *)NULL,\n");
  158.         FPrintf(IconFile,"\t%ld,%ld,\n",Drawer -> dd_NewWindow . MinWidth,Drawer -> dd_NewWindow . MinHeight);
  159.         FPrintf(IconFile,"\t%ld,%ld,\n",Drawer -> dd_NewWindow . MaxWidth,Drawer -> dd_NewWindow . MaxHeight);
  160.         FPrintf(IconFile,"\t%ld,\n\n",Drawer -> dd_NewWindow . Type);
  161.  
  162.         FPrintf(IconFile,"\t%ld,\n",Drawer -> dd_CurrentX);
  163.         FPrintf(IconFile,"\t%ld,\n",Drawer -> dd_CurrentY);
  164.  
  165.         FPrintf(IconFile,"#ifdef OLDDRAWERDATAFILESIZE\n");
  166.         FPrintf(IconFile,"\t0x%08lx,\n",Drawer -> dd_Flags);
  167.         FPrintf(IconFile,"\t0x%04lx\n",Drawer -> dd_ViewModes);
  168.         FPrintf(IconFile,"#endif\t/* OLDDRAWERDATAFILESIZE */\n");
  169.         FPrintf(IconFile,"};\n\n");
  170.     }
  171.  
  172.         /* Create icon tool types. */
  173.  
  174.     if(ToolTypes && *ToolTypes)
  175.     {
  176.         Puts("\tCreating icon tool types...");
  177.  
  178.         FPrintf(IconFile,"char *%sToolTypes[] =\n{\n",Name);
  179.  
  180.         while(*ToolTypes)
  181.             FPrintf(IconFile,"\t\"%s\",\n",*ToolTypes++);
  182.  
  183.         FPrintf(IconFile,"\tNULL\n};\n\n");
  184.     }
  185.  
  186.     Puts("\tCreating icon object...");
  187.  
  188.         /* Create the icon structure. */
  189.  
  190.     FPrintf(IconFile,"struct DiskObject %sIcon =\n{\n",Name);
  191.     FPrintf(IconFile,"\tWB_DISKMAGIC,\n");
  192.     FPrintf(IconFile,"\tWB_DISKVERSION,\n\n");
  193.  
  194.     FPrintf(IconFile,"\t(struct Gadget *)NULL,\n");
  195.     FPrintf(IconFile,"\t%ld,%ld,\n",Icon -> do_Gadget . LeftEdge,Icon -> do_Gadget . TopEdge);
  196.     FPrintf(IconFile,"\t%ld,%ld,\n",Icon -> do_Gadget . Width,Icon -> do_Gadget . Height);
  197.  
  198.     FPrintf(IconFile,"\t0x%04lx,\n",Icon -> do_Gadget . Flags);
  199.     FPrintf(IconFile,"\t0x%04lx,\n",Icon -> do_Gadget . Activation);
  200.     FPrintf(IconFile,"\t0x%04lx,\n",Icon -> do_Gadget . GadgetType);
  201.  
  202.     FPrintf(IconFile,"\t(APTR)&%s1Image,\n",Name);
  203.  
  204.         /* Did it have an alternate image? */
  205.  
  206.     if(Icon -> do_Gadget . Flags & GADGHIMAGE)
  207.         FPrintf(IconFile,"\t(APTR)&%s2Image,\n",Name);
  208.     else
  209.         FPrintf(IconFile,"\t(APTR)NULL,\n");
  210.  
  211.     FPrintf(IconFile,"\t(struct IntuiText *)NULL,\n");
  212.     FPrintf(IconFile,"\tNULL,\n");
  213.     FPrintf(IconFile,"\t(APTR)NULL,\n");
  214.     FPrintf(IconFile,"\tNULL,\n");
  215.     FPrintf(IconFile,"\t(APTR)NULL,\n\n");
  216.  
  217.     FPrintf(IconFile,"\t%s,\n",WB_Types[Icon -> do_Type - 1]);
  218.  
  219.         /* Create default tool. */
  220.  
  221.     if(Icon -> do_DefaultTool)
  222.         FPrintf(IconFile,"\t\"%s\",\n",Icon -> do_DefaultTool);
  223.     else
  224.         FPrintf(IconFile,"\t(char *)NULL,\n");
  225.  
  226.         /* Add the tool types if necessary. */
  227.  
  228.     if(Icon -> do_ToolTypes && *Icon -> do_ToolTypes)
  229.         FPrintf(IconFile,"\t%sToolTypes,\n",Name);
  230.     else
  231.         FPrintf(IconFile,"\tNULL,\n");
  232.  
  233.     FPrintf(IconFile,"\tNO_ICON_POSITION,\n");
  234.     FPrintf(IconFile,"\tNO_ICON_POSITION,\n");
  235.  
  236.         /* Add the drawerdata. */
  237.  
  238.     if(Drawer && (Icon -> do_Type == WBDISK || Icon -> do_Type == WBDRAWER || Icon -> do_Type == WBGARBAGE))
  239.         FPrintf(IconFile,"\t(struct DrawerData *)&%sDrawer,\n",Name);
  240.     else
  241.         FPrintf(IconFile,"\t(struct DrawerData *)NULL,\n");
  242.  
  243.     if(Icon -> do_ToolWindow)
  244.         FPrintf(IconFile,"\t\"%s\",\n",Icon -> do_ToolWindow);
  245.     else
  246.         FPrintf(IconFile,"\t(char *)NULL,\n");
  247.  
  248.     FPrintf(IconFile,"\t%ld\n};\n\n",Icon -> do_StackSize);
  249.  
  250.         /* At last, add a macro to save the icon. */
  251.  
  252.     FPrintf(IconFile,"#define Save%sIcon(Destination) PutDiskObject(Destination,&%sIcon)\n",Name,Name);
  253. }
  254.  
  255.     /* main(int argc,char **argv):
  256.      *
  257.      *    The main program.
  258.      */
  259.  
  260. VOID
  261. main(int argc,char **argv)
  262. {
  263.     BYTE ErrorCode = RETURN_FAIL;
  264.  
  265.     Puts("\n\33[1m\33[33mIcon2C V1.2 \33[0m\33[31mCopyright © 1990 by MXM, all rights reserved\n");
  266.  
  267.     if(argv[ARG_INFO])
  268.     {
  269.         Puts("This  program  converts  icon  info  files into standard 'C'");
  270.         Puts("source code.\n");
  271.  
  272.         Puts("Author: Olaf Barthel, MXM");
  273.         Puts("        Brabeckstrasse 35");
  274.         Puts("        D-3000 Hannover 71\n");
  275.  
  276.         Puts("        Federal Republic of Germany\n");
  277.  
  278.         Puts("Written at Hannover, Monday 25-Jun-90.\n");
  279.  
  280.         Puts("This  program  is  Share-Ware,  if  you  like  it and use it");
  281.         Puts("frequently  a  small  donation  will  entitle you to receive");
  282.         Puts("updates and new programs from MXM.\n");
  283.  
  284.         exit(RETURN_OK);
  285.     }
  286.  
  287.     if(!argv[ARG_FROM] || !argv[ARG_TO])
  288.     {
  289.         Puts(CLI_Help);
  290.         exit(RETURN_WARN);
  291.     }
  292.  
  293.     Printf("Opening icon.library... ");
  294.  
  295.     if(IconBase = (struct Library *)ArpOpenLibrary("icon.library",0))
  296.     {
  297.         struct DiskObject    *IconData;
  298.         BPTR             IconFile;
  299.  
  300.         Printf(" Reading icon file... ");
  301.  
  302.         if(IconData = GetDiskObject(argv[ARG_FROM]))
  303.         {
  304.             Printf("creating output file... ");
  305.  
  306.             if(IconFile = ArpOpen(argv[ARG_TO],MODE_NEWFILE))
  307.             {
  308.                 Icon2C(IconData,BaseName(argv[ARG_FROM]),IconFile);
  309.  
  310.                 ErrorCode = RETURN_OK;
  311.  
  312.                 Puts("\nIcon file created OK!\n");
  313.             }
  314.             else
  315.                 Printf("\33[1mERROR\33[0m (%ld): cannot create output file!\a\n",IoErr());
  316.  
  317.             FreeDiskObject(IconData);
  318.         }
  319.         else
  320.             Printf("\33[1mERROR\33[0m (%ld): cannot read icon file!\a\n",IoErr());
  321.     }
  322.     else
  323.         Printf("\33[1mERROR\33[0m (%ld): cannot open icon.library!\a\n",IoErr());
  324.  
  325.     exit(ErrorCode);
  326. }
  327.